package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxd;
import cn.stylefeng.guns.sys.modular.system.entity.Huopei;
import cn.stylefeng.guns.sys.modular.system.model.HuopeiDto;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdService;
import cn.stylefeng.guns.sys.modular.system.service.HuopeiService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 货赔管理控制器
 *
 */
@Controller
@RequestMapping("/huopei")
public class HuopeiController extends BaseController {

    private String PREFIX = "/modular/system/huopei/";

    @Autowired
    private HuopeiService huopeiService;
    @Resource
    private BtyBxdService btyBxdService;

    /**
     * 跳转到校验银行卡号页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "huopei.html";
    }

    /**
     * 查询列表
     *
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(Huopei huopei, String timeLimit) {

//        String startTime;
//        String endTime;

//        if (ToolUtil.isNotEmpty(timeLimit)){
//            String[] split = timeLimit.split(" - ");
//            startTime = split[0] + "-01";
//            endTime = split[1] + "-01";
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
//            Date start = null;
//            Date end = null;
//
//            try {
//                start = format.parse(startTime);
//                end = format.parse(endTime);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//
//            huopei.setBeginTime(start);
//            huopei.setEndTime(end);
//        }

        //当前登陆者姓名
        String name = LoginContextHolder.getContext().getUser().getName();
        //当前登陆者角色
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        LayuiPageInfo list = new LayuiPageInfo();

        if (roleNames.contains("会计")){
            if (ToolUtil.isEmpty(huopei.getShopName())){
                //查询该会计名下的所有店铺
                List dm = huopeiService.dmList(name);
//                list = huopeiService.list(dm, huopei.getBeginTime(), huopei.getEndTime());
                list = huopeiService.list(dm, timeLimit);
            }else {
                list = huopeiService.huopeiList(huopei, timeLimit);

            }

            return list;
        }else if (roleNames.contains("超级管理员") || roleNames.contains("人事") || roleNames.contains("财务主管")){
            return this.huopeiService.huopeiList(huopei, timeLimit);

            //店长
        }else if(roleNames.contains("店长报销单管理")){
            if (ToolUtil.isEmpty(huopei.getShopName())){
                //查询店长的店铺
                List dz = huopeiService.dzList(name);
//                list = huopeiService.list(dz, huopei.getBeginTime(), huopei.getEndTime());
                list = huopeiService.list(dz, timeLimit);
            }else {
                list = huopeiService.huopeiList(huopei, timeLimit);

            }

            return list;
        }
        return list;
    }


    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUser(@RequestParam("file") MultipartFile file) {

        String fileName = file.getOriginalFilename();//文件名
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = huopeiService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;

    }


    // 导出excel
    @RequestMapping("/listExport")
    public ResponseEntity<byte[]> exportExcel(HttpServletRequest request, HttpServletResponse response,
                                              String shopName, Date beginTime, String shopCode, String date) {
        return this.huopeiService.exportExcel(request, response, shopName, beginTime, shopCode, date);
    }


    /**
     * 跳转到各店展示页面
     *
     */
    @RequestMapping("/detail")
    public String detail(Model model, Huopei huopei, String date) {

        List<Huopei> list = huopeiService.getHuopei(huopei, date);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begin = list.get(0).getBeginTime();
        Date end = list.get(0).getEndTime();
        String beginTime = format.format(begin);
        String endTime = format.format(end);

        model.addAttribute("beginTime", beginTime);//开始时间
        model.addAttribute("endTime", endTime);//结束时间
        model.addAttribute("shopCode", list.get(0).getShopCode());//门店编号
        model.addAttribute("shopName", list.get(0).getShopName());//门店名称
        model.addAttribute("turnover",list.get(0).getTurnover());//营业额
        model.addAttribute("five",list.get(0).getFive());//营业额万分之五
        model.addAttribute("ten",list.get(0).getTen());//营业额万分之十

        //当前登陆者角色
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        model.addAttribute("roleNames", roleNames);
        if (roleNames.contains("会计")){
            //会计
            model.addAttribute("status", 1);
        }else if(roleNames.contains("店长报销单管理")){
            //店长
            model.addAttribute("status", 2);
        }else if (roleNames.contains("人事")) {
            //人事
            model.addAttribute("status", 3);
        } else{
            model.addAttribute("status", 0);
        }

        return PREFIX + "/detail.html";
    }


    /**
     * 各门店货赔详细信息
     *
     */
    @RequestMapping("/huopeiInfo")
    @ResponseBody
    public LayuiPageInfo huopeiInfo(Huopei huopei, String date) {
        //查询列表数据
        List<Huopei> huopeiList = this.huopeiService.getHuopei(huopei,date);
        int total = huopeiList.size();
        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(huopeiList);
        layuiPageInfo.setCount(total);
        return layuiPageInfo;
    }


    /**
     * 店长审核页面
     *
     */
    @RequestMapping("/manager")
    public String manager() {
        return PREFIX + "/manager.html";
    }

    /**
     * 审核
     *
     */
    @ResponseBody
    @RequestMapping("/audit")
    public Object audit(Huopei huopei) {
        int num = huopeiService.audit(huopei);
        return num;
    }

    /**
     * 编辑接口
     *
     */
    @RequestMapping("/editHuopei")
    @ResponseBody
    public ResponseData editHuopei(HuopeiDto huopeiDto) {
        this.huopeiService.editHuopei(huopeiDto);
        return ResponseData.success();
    }


    /**
     * 会计通知店长提交完成
     *
     */
    @RequestMapping("/remind")
    @ResponseBody
    public Object remind(Huopei huopei) {
        int num = this.huopeiService.remind(huopei);
        return num;
    }



    /**
     * 查询店名
     *
     * @author liuy
     *
     */
    @RequestMapping(value = "/getDm")
    @ResponseBody
    public List<BtyBxd> getDictionaryType(){
        //获取是否是督导
        String rem = LoginContextHolder.getContext().getUser().getAvatar();
        //当前登陆者姓名
        String account = LoginContextHolder.getContext().getUser().getName();
        //账号
        String username = LoginContextHolder.getContext().getUser().getAccount();
        //当前登陆者角色集
        List<String> roleNames = LoginContextHolder.getContext().getUser().getRoleNames();
        List<BtyBxd> list = null;

        if (account.equals("admin") || roleNames.contains("出纳") || username.equals("chujunjun")
                || roleNames.contains("人事") || roleNames.contains("财务主管")) {
            list = this.btyBxdService.dmList(null, null);

        } else if(roleNames.contains("会计")){
            list = huopeiService.kjList(account);

        }else if (roleNames.contains("店长报销单管理")){
            list = huopeiService.listDz(account);
        }

        return list;



//        if(!(userName.equals(account))){
//            if(rem.contains(off)){
//                list = this.btyWageService.dmList(account,null,null);
//                if(list.size()==0){
//                    list = this.btyWageService.dmList(null,null,phone);
//                }
//            }else{
//                list = this.btyWageService.dmList(null,account,null);
//                if(list.size()==0){
//                    list = this.btyWageService.dmList(null,account,phone);
//                }
//            }
//        }else{
//            list = this.btyWageService.dmList(account,null,null);
//        }
//
//        return list;
    }
}
